package cn.com.wantools.web.api.wantools;

import cn.com.wantools.commons.Rets;
import cn.com.wantools.model.unitconvert.UcConvertTable;
import cn.com.wantools.model.unitconvert.UcConvertType;
import cn.com.wantools.model.unitconvert.UcUnitType;
import cn.com.wantools.service.UcConvertTableService;
import cn.com.wantools.service.UcConvertTypeService;
import cn.com.wantools.service.UcUnitTypeService;
import cn.com.wantools.web.base.ApiControllerBase;
import com.jfinal.aop.Inject;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.Ret;
import io.jboot.apidoc.ContentType;
import io.jboot.apidoc.annotation.Api;
import io.jboot.apidoc.annotation.ApiOper;
import io.jboot.web.controller.annotation.RequestMapping;

import javax.validation.constraints.NotEmpty;
import java.util.List;

@RequestMapping("/api/unitConvert")
@Api("单位换算相关API ")
public class UnitConvertApiController extends ApiControllerBase {
    @Inject
    private UcUnitTypeService ucUnitTypeService;

    @Inject
    private UcConvertTableService ucConvertTableService;

    @Inject
    private UcConvertTypeService ucConvertTypeService;

    @ApiOper(value = "查询单位换算类别列表", contentType = ContentType.JSON)
    public Ret queryConvertTypeList() {
        try {
            List<UcConvertType> convertTypeList = ucConvertTypeService.queryAllConvertType();
            return Ret.create("convertTypeList", convertTypeList);
        } catch (Exception e) {
            return Rets.FAIL;
        }
    }

    @ApiOper(value = "查询单位列表", contentType = ContentType.JSON)
    public Ret queryUnitTypeListByConvertType(@NotEmpty String convertType) {
        try {
            List<UcUnitType> unitTypeList = ucUnitTypeService.queryUnitTypeByConvertType(convertType);
            return Ret.create("unitTypeList", unitTypeList);
        } catch (Exception e) {
            return Rets.FAIL;
        }
    }

    @ApiOper(value = "查询单位换算关系表", contentType = ContentType.JSON)
    public Ret queryConvertTableListByConvertType(@NotEmpty String convertType) {
        try {
            List<UcConvertTable> convertTableList = ucConvertTableService.queryConvertTableByConvertType(convertType);
            return Ret.create("convertTableList", convertTableList);
        } catch (Exception e) {
            LogKit.warn("queryConvertTableListByConvertType failed， error：", e);
            e.printStackTrace();
            return Rets.FAIL;
        }
    }
}
